﻿#pragma once
#include "ThostFtdcTraderApi.h"
#include <functional>
#include <atomic>
#include <list>
#include "TradeBlocker.h"

struct CTPUserData
{
	std::string UserId;
	std::string User_PWD;
	std::string User_BrokerID; // 经纪公司代码
	std::string U_AuthCode; // 认证码
	std::string User_ProductInfo; // 用户端产品信息
	std::string U_AppID; // App代码
	std::string U_InvestorID; // 投资者代码
	std::list<std::string> Trade_IPs;

};

class TradeApiSpi : public CThostFtdcTraderSpi
{
public:
	TradeApiSpi(CThostFtdcTraderApi *tradeApi);
	~TradeApiSpi();

	// 返回合约列表的回调
	void SetRspQryInstrumentFunc(std::function<void(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)> pFunc);
	// 持仓查询回调
	void SetRspQryInvestorPositionFunc(std::function<void(CThostFtdcInvestorPositionField *position, CThostFtdcRspInfoField *rsp_info, int req_id, bool is_last)> pFunc);
	// 账户查询的回调
	void SetRspQryTradingAccountFunc(std::function<void(CThostFtdcTradingAccountField *trading_account, CThostFtdcRspInfoField *rsp_info, int req_id, bool is_last)> pFunc);
	// 设置下单成交回调
	void SetTradeBackFunc(std::function<void(CThostFtdcTradeField *pTrade)> pFunc);

	// 返回账户信息
	const CTPUserData &GetCtpUsr() { return m_ctpUsr; }

	// 返回CTP是否认证通过
	bool GetAuthenticateSuc() { return m_AuthenticateSuc; }

	// 设置阻塞器
	void SetBlocker(BlockerSmart pBlocker) { m_blocker = pBlocker; }
protected:
	//连接
	virtual void OnFrontConnected() override;
	virtual void OnFrontDisconnected(int nReason) override;

	//认证
	virtual void OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	// 登录回调
	virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	//请求查询投资者结算结果响应
	virtual void OnRspQrySettlementInfo(CThostFtdcSettlementInfoField *pSettlementInfo, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

	virtual void OnRspQryInvestor(CThostFtdcInvestorField *pInvestor, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRspUserPasswordUpdate(CThostFtdcUserPasswordUpdateField *pUserPasswordUpdate, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

	//下单
	virtual void OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnErrRtnOrderInsert(CThostFtdcInputOrderField *pInputOrder, CThostFtdcRspInfoField *pRspInfo) override;

	//撤单
	virtual void OnRspOrderAction(CThostFtdcInputOrderActionField *pInputOrderAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnErrRtnOrderAction(CThostFtdcOrderActionField *pOrderAction, CThostFtdcRspInfoField *pRspInfo) override;

	//报单回报
	virtual void OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRtnOrder(CThostFtdcOrderField *pOrder) override;

	// 请求查询成交响应，当执行ReqQryTrade后，该方法被调用
	virtual void OnRspQryTrade(CThostFtdcTradeField *pTrade, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	//成交回报
	virtual void OnRtnTrade(CThostFtdcTradeField *pTrade) override;

	//报价录入
	virtual void OnRspQuoteInsert(CThostFtdcInputQuoteField *pInputQuote, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnErrRtnQuoteInsert(CThostFtdcInputQuoteField *pInputQuote, CThostFtdcRspInfoField *pRspInfo) override;
	virtual void OnRspQryQuote(CThostFtdcQuoteField *pQuote, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRtnQuote(CThostFtdcQuoteField *pQuote) override;

	//报价撤单
	virtual void OnRspQuoteAction(CThostFtdcInputQuoteActionField *pInputQuoteAction, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnErrRtnQuoteAction(CThostFtdcQuoteActionField *pQuoteAction, CThostFtdcRspInfoField *pRspInfo) override;

	virtual void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

	//仓位
	virtual void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *pInvestorPosition, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

	//资金
	virtual void OnRspQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;

	//合约、手续费
	virtual void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) override;
	//其它
	virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
	virtual void OnRtnInstrumentStatus(CThostFtdcInstrumentStatusField *pInstrumentStatus) override;

private:
	bool isErrorRspInfo(CThostFtdcRspInfoField *pRspInfo);

	// 合约的返回操作
	std::function<void(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)> m_RspQryInstrumentFunc;
	// 持仓查询回调
	std::function<void(CThostFtdcInvestorPositionField *position, CThostFtdcRspInfoField *rsp_info, int req_id, bool is_last)> m_RspQryInvestorPositionFunc;
	// 账户查询回调
	std::function<void(CThostFtdcTradingAccountField *trading_account, CThostFtdcRspInfoField *rsp_info, int req_id, bool is_last)> m_RspQryTradingAccountFunc;
	// 下单成交回调
	std::function<void(CThostFtdcTradeField *pTrade)> m_TradeFeedFunc;

	CThostFtdcTraderApi *m_api;
	CTPUserData m_ctpUsr;
	//CTPSemaphore &m_waitSemaphor; // 是否需要阻塞的信号量
	BlockerSmart m_blocker;
	bool m_AuthenticateSuc;
};

